package com.xjoyt.pay.share.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.xjoyt.pay.abstracts.AbstractDao;
import com.xjoyt.pay.api.ali.OssUtils;
import com.xjoyt.pay.share.dao.FileObejctRepositoryCustom;
import com.xjoyt.pay.share.po.FileObejct;
import com.xjoyt.pay.share.repository.FileObejctRepository;
import com.xjoyt.pay.share.vo.FileObjecResult;
import com.xjoyt.pay.share.vo.FileObjectParam;

public class FileObejctRepositoryImpl extends AbstractDao implements FileObejctRepositoryCustom {

	@Autowired
	private FileObejctRepository repository;

	@Override
	public List<FileObjecResult> findAllByKeysAndProperties(FileObjectParam param) {
		List<FileObejct> list = repository.findAllByKeyInAndPropertyIn(param.keys, param.properties);
		List<FileObjecResult> result = new ArrayList<>();
		list.forEach(item -> {
			String url = OssUtils.getUrl(item.bucket.endpoint, item.bucket.accessKey, item.bucket.secretKey,
					item.bucket.name, item.path);
			if (StringUtils.isNotBlank(url)) {
				FileObjecResult vo = new FileObjecResult();
				result.add(vo);
				vo.url = url;
				vo.id = item.key;
				vo.property = item.property;
			}
		});

		return result;

	}

}
